<?php
/**
 * Nweb Editor
 *
 * This script is protected by copyright. Its use, copying, modification
 * and distribution without written consent of the author is prohibited.
 *
 * @category    source
 * @package     pl.nweb.editor.util
 * @author      Krzysztof Kardasz <krzysztof.kardasz@gmail.com>
 * @copyright   Copyright (c) 2012 Krzysztof Kardasz
 * @license     http://www.gnu.org/licenses/lgpl-3.0.txt  GNU Lesser General Public
 * @version     1.0 $Id$
 * @link        https://nweb-editor.googlecode.com
 * @link        http://editor.nweb.pl
 */

namespace pl\nweb\editor\util\session;

/**
 * Unikalne id w ramach sesji
 *
 * @category    source
 * @package     pl.nweb.editor.util
 * @author      Krzysztof Kardasz <krzysztof.kardasz@gmail.com>
 * @copyright   Copyright (c) 2012 Krzysztof Kardasz
 * @version     1.0 $Id$
 */
class UniqueId
{
    /**
     * Zwraca unikalny identyfikator
     *
     * @param integer
     */
    public static function getId ()
    {
        return self::getNamespaceId ('default');
    }

    /**
     * Zwraca unikalny identyfikator
     *
     * @param string $namespace przestrzeń nazw
     * @param integer
     */
    public static function getNamespaceId ($namespace = 'default')
    {
        $hash = 'pl.nweb.editor.util.unique.id::' . sha1($namespace);
        if(!isset($_SESSION[$hash]) || !is_numeric($_SESSION[$hash])) {
            return $_SESSION[$hash] = 1;
        }
        return ++$_SESSION[$hash];
    }
}